package com.icongyou.enterprise.talent_analysis.entity.dto;

import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生特征上下文 - 用于AI分析的完整学生数据
 * 包含数值指标、文本描述、任务详情等多维度信息
 */
@Data
public class StudentFeatureContext {
    
    // === 基础信息 ===
    private String studentId;
    private String studentName;
    private String major;
    private String className;
    
    // === 数值型指标 ===
    private Map<String, Object> numericMetrics = new HashMap<>();
    
    // === 任务维度数据 ===
    private List<AssignmentFeature> assignments = new ArrayList<>();
    
    // === 教师评价文本 ===
    private List<TeacherComment> teacherComments = new ArrayList<>();
    
    // === 团队协作数据 ===
    private List<TeamExperience> teamExperiences = new ArrayList<>();
    
    // === 学习行为数据 ===
    private LearningBehavior learningBehavior = new LearningBehavior();
    
    // === 统计摘要 ===
    private StatisticalSummary summary = new StatisticalSummary();
    
    /**
     * 任务特征
     */
    @Data
    public static class AssignmentFeature {
        private String assignmentId;
        private String assignmentName;
        private String assignmentType; // 个人/团队
        private String teamId;
        private BigDecimal fullScore;
        private BigDecimal finalScore;
        private String taskDescription; // 任务要求描述
        private String gradingCriteria; // 评分标准
        private Integer expectedDuration;
        private Integer actualDuration;
        private Boolean isLate;
        private Integer likeCount;
        private Integer favoriteCount;
        private BigDecimal contributionRate; // 团队任务中该学生的贡献度（0-100）
        private String teacherTextComment; // 教师文字评语
        private String aiComment; // AI评语
        private Boolean isTeacherRecommended; // 是否教师推荐
    }
    
    /**
     * 教师评价
     */
    @Data
    public static class TeacherComment {
        private String assignmentName;
        private String commentText;
        private BigDecimal score;
        private String commentTime;
        private Boolean isRecommended;
    }
    
    /**
     * 团队经历
     */
    @Data
    public static class TeamExperience {
        private String teamName;
        private BigDecimal teamScore;
        private Integer teamRanking;
        private BigDecimal avgContribution;
        private Integer taskCount;
    }
    
    /**
     * 学习行为
     */
    @Data
    public static class LearningBehavior {
        private Integer totalSubmissions;
        private Integer onTimeSubmissions;
        private Integer lateSubmissions;
        private Integer discussionCount;
        private Integer highScoreCount; // 高分作业数
        private Integer reworkCount; // 返工次数
        private Double avgCompletionSpeed; // 平均完成速度
    }
    
    /**
     * 统计摘要
     */
    @Data
    public static class StatisticalSummary {
        private BigDecimal avgScore;
        private BigDecimal maxScore;
        private BigDecimal minScore;
        private BigDecimal scoreStdDev; // 成绩标准差
        private Integer totalCourses;
        private Integer totalAssignments;
        private Integer completedAssignments;
        private Double onTimeRate;
        private Double passRate;
    }
    
    /**
     * 转换为AI可读的文本描述
     */
    public String toAIReadableText() {
        StringBuilder sb = new StringBuilder();
        sb.append("【学生基础信息】\n");
        sb.append(String.format("姓名: %s | 专业: %s | 班级: %s\n\n", studentName, major, className));
        
        sb.append("【学习表现摘要】\n");
        sb.append(String.format("平均分: %.2f | 最高分: %.2f | 完成率: %.1f%% | 按时率: %.1f%%\n\n", 
            summary.getAvgScore(), summary.getMaxScore(), 
            (summary.getCompletedAssignments() * 100.0 / summary.getTotalAssignments()),
            summary.getOnTimeRate() * 100));
        
        sb.append("【代表性任务详情】\n");
        for (int i = 0; i < Math.min(5, assignments.size()); i++) {
            AssignmentFeature a = assignments.get(i);
            sb.append(String.format("任务: %s | 得分: %.2f/%.2f | 用时: %dmin/%dmin | 教师评价: %s\n",
                a.getAssignmentName(), a.getFinalScore(), a.getFullScore(),
                a.getActualDuration(), a.getExpectedDuration(),
                a.getTeacherTextComment() != null ? a.getTeacherTextComment() : "无"));
        }
        
        sb.append("\n【教师评价精选】\n");
        for (int i = 0; i < Math.min(3, teacherComments.size()); i++) {
            TeacherComment c = teacherComments.get(i);
            sb.append(String.format("- [%s] %s\n", c.getAssignmentName(), c.getCommentText()));
        }
        
        if (!teamExperiences.isEmpty()) {
            sb.append("\n【团队协作经历】\n");
            for (TeamExperience te : teamExperiences) {
                double teamScore = te.getTeamScore() != null ? te.getTeamScore().doubleValue() : 0d;
                double avgContribution = te.getAvgContribution() != null ? te.getAvgContribution().doubleValue() : 0d;
                double normalizedContribution = avgContribution > 1 ? avgContribution : avgContribution * 100;
                int taskCount = te.getTaskCount() != null ? te.getTaskCount() : 0;
                sb.append(String.format("团队: %s | 团队分: %.2f | 平均贡献: %.2f%% | 参与任务: %d\n",
                    te.getTeamName(), teamScore, normalizedContribution, taskCount));
            }
        }
        
        return sb.toString();
    }
}
